package com.example.stock.service.impl;

import com.example.stock.mapper.GbStaMapper;
import com.example.stock.model.GBSta;
import com.example.stock.service.GBStaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GBStaServiceImpl implements GBStaService {

    public static final String GB_15W_K = "15w_gb";
    public static final String GB_20W_K = "20w_gb";
    public static final String GB_30W_K = "30w_gb";
    public static final String GB_60W_K = "60w_gb";

    @Autowired
    GbStaMapper gbStaMapper;

    @Override
    public List<GBSta> getGBStaList(String type) {
        return gbStaMapper.getGBStaDataByType(type);
    }

    @Override
    public List<GBSta> getLastGBStaList(String type, int count) {
        return getGBStaList(type).stream().sorted(Comparator.comparing(GBSta::getDate).reversed()).limit(count).toList();
    }

    @Override
    public List<GBSta> getGBStaByStock(String stock, String type) {
        return gbStaMapper.getGBStaDataByStockAndType(stock, type).stream()
                .filter(i -> i.getStock().equals(stock))
                .toList();
    }

    @Override
    public List<GBSta> getGB60WStaByDate(String date) {
        return getGBStaByDate(date, GB_60W_K);
    }

    @Override
    public List<GBSta> getGBStaByDate(String date, String type) {
        return gbStaMapper.getGBStaDataByDateAndType(date, type);
    }

    @Override
    public List<GBSta> getGBStaUpByDate(String date, String type) {
        List<GBSta> gbStaList = getGBStaByDate(date, type);

        return switch (type) {
            case GB_15W_K -> gbStaList.stream().filter(i -> i.getClose() >= i.getGb15w()).toList();
            case GB_20W_K -> gbStaList.stream().filter(i -> i.getClose() >= i.getGb20w()).toList();
            case GB_30W_K -> gbStaList.stream().filter(i -> i.getClose() >= i.getGb30w()).toList();
            case GB_60W_K -> gbStaList.stream().filter(i -> i.getClose() >= i.getGb60w()).toList();
            default -> null;
        };
    }

    /**
     * 突破
     * @param type
     * @return
     */
    @Override
    public Map<String, List<GBSta>> getGBUpStaMapByDate(String type) {
        return getGBStaList(type).stream()
                .filter(i -> i.getDisPct() >= 0)
                .collect(Collectors.groupingBy(GBSta::getDate));
    }

    @Override
    public Map<String, List<GBSta>> getGBDownStaMapByDate(String type) {
        return getGBStaList(type).stream()
                .filter(i -> i.getDisPct() < 0)
                .collect(Collectors.groupingBy(GBSta::getDate));
    }

    @Override
    public List<GBSta> getDurGtList(String type, int num) {
        return gbStaMapper.getDurGtNum(type, num)
                .stream().sorted(Comparator.comparing(GBSta::getDate).reversed())
                .toList();
    }
}